<?php
namespace App\Logics;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


use App\Libs\Vendor\AliPaySdk\aop\AopClient;
use App\Libs\Vendor\AliPaySdk\aop\request\AlipaySecurityRiskCustomerriskQueryRequest;

//逻辑处理
class XxhfLogic extends BaseLogic {


    public function __construct()
    {
        parent::__construct();

    }

    /** 获取先享后付检查结果
     * @param array $da
     * @return array
     * @throws \Exception
     */
    public static function getXxhf(array $da){
        if( !isset($da['user_phone']) ||
            !isset($da['user_id_number']) ||
            !isset($da['order_gross_rent']) ||
            !isset($da['zm_user_id'])
         ) return error('参数不全');

        $aop = new AopClient();
        $pid=$aop->pid;
        $request = new AlipaySecurityRiskCustomerriskQueryRequest();
        $data=[
            'risk_type'=>'riskinfo_nsf_common',
            'mobile_no'=>$da['user_phone']??'null',
            'cert_no'=>$da['user_id_number']??'null',
            'service'=>'nsf',
            'pid'=>$pid,
            'sales_amount'=>$da['order_gross_rent'],
            'user_id'=>$da['zm_user_id']??'null'
        ];
        $request->setBizContent(json_encode($data));
        $result = $aop->execute ( $request);


        if(empty($result['alipay_security_risk_customerrisk_query_response']))return error('风险查询失败');
        if($result['alipay_security_risk_customerrisk_query_response']['code']!=10000)return error('风险查询失败');
        $risk_result=json_decode($result['alipay_security_risk_customerrisk_query_response']['risk_result'],true);
        $risk_result_desc=json_decode($result['alipay_security_risk_customerrisk_query_response']['risk_result_desc'],true);
        if(isset($risk_result['errorCode']))return error($result['alipay_security_risk_customerrisk_query_response']['risk_result_desc']);
        $data=[
            'rank0'=>'提供信息不足或提供参数信息有误',
            'rank1'=>'用户拒付风险指数：低',
            'rank2'=>'用户拒付风险指数：中',
            'rank3'=>'用户拒付风险指数：高'
        ];

        $arr=[
            'res'=>$data[array_keys($risk_result_desc)[0]]??'未知错误',
            'json_res'=>json_encode($result['alipay_security_risk_customerrisk_query_response'])
        ];
        return success('ok',$arr);
    }






}
